package com.srd.phone.mvp.presenters;


import android.text.TextUtils;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.srd.phone.application.HttpStringRequest;
import com.srd.phone.mvp.interfaces.IBaseView;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RetrievePwdPresenter extends BasePresenter {

    public RetrievePwdPresenter(RequestQueue requestQueue, IBaseView iView0) {
        super(requestQueue,iView0);
    }

    //本地验证：手机号码，邮箱号码非空验证
    public String checkLocalUser(boolean isRequestPhone, String phone, String email) {
        if (isRequestPhone && TextUtils.isEmpty(phone)) {
            return "手机号码不能为空";
        } else if (isRequestPhone == false && TextUtils.isEmpty(email)) {
            return "邮箱号码不能为空";
        }
        if (isRequestPhone) {
            if (checkPhone(phone) == false) {
                return "手机号码输入有误";
            }
        } else {
            if (checkEmail(email) == false) {
                return "邮箱号输入有误";
            }
        }
        return "";
    }

    //本地验证：验证码非空验证
    public String checkLocalCode(boolean isRequestPhone, String phone, String email, String code) {
        if (isRequestPhone && TextUtils.isEmpty(phone)) {
            return "手机号码不能为空";
        } else if (isRequestPhone == false && TextUtils.isEmpty(email)) {
            return "邮箱号码不能为空";
        }
        if (TextUtils.isEmpty(code)) {
            return "验证码不能为空";
        }
        if (isRequestPhone) {
            if (checkPhone(phone) == false) {
                return "手机号码输入有误";
            }
        } else {
            if (checkEmail(email) == false) {
                return "邮箱号输入有误";
            }
        }
        return "";
    }

    /**
     * /网络请求：判断用户是否存在请求
     */
    public void checkUserExists(final String url, final String value) {
        HttpStringRequest request = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("input", value);
                return map;
            }
        };
        request.setTag(url);
        requestQueue.add(request);
    }


    /**
     * 网络请求：请求手机或者邮箱验证码
     */
    public void sendCheckCode(final String url, final String value) {
        HttpStringRequest codeRequest = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("value", value);
                return map;
            }
        };
        codeRequest.setTag(url);
        requestQueue.add(codeRequest);
    }

    /**
     * 点击下一步，验证提交
     * @param url
     * @param value
     * @param vc
     */
    public void checkCode(final String url, final String value, final String vc) {
        HttpStringRequest request = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("input", value);
                map.put("vc", vc);
                return map;
            }
        };
        request.setTag(url);
        requestQueue.add(request);
    }

    /**
     * 验证邮箱
     */
    public boolean checkEmail(String email) {
        boolean flag = false;
        try {
            String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(email);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    /**
     * 验证手机号
     */
    public boolean checkPhone(String phone) {
        if (TextUtils.isEmpty(phone) || phone.length() != 11)
            return false;
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(phone);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }
}
